package top.continew.admin.business.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import net.dreamlu.mica.core.utils.DesensitizationUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.continew.admin.business.service.UserHelperService;
import top.continew.admin.system.mapper.UserRoleMapper;
import top.continew.admin.system.mapper.user.UserMapper;
import top.continew.admin.system.model.entity.user.UserDO;
import top.continew.admin.system.model.vo.UserInfoVo;
import top.continew.admin.system.model.vo.UserWithCpVo;
import top.continew.admin.system.model.vo.UserWithRoleVo;

import java.util.List;

/**
 * @program: examstar-manger-api
 * @description: 用户的辅助业务实现
 * @author: Xiong weiqiang
 * @create: 2025-03-27 17:04
 */
@Service
public class UserHelperServiceImpl implements UserHelperService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Override
    public List<UserWithCpVo> selectUserWithCpByPhone(String phone){
        List<UserWithCpVo> list= userMapper.selectUserWithCpByPhone(phone);
        if (CollUtil.isNotEmpty(list)){
            list.forEach(a->a.setPhone(DesensitizationUtil.mobileNo(a.getPhone())));
        }
        return CollUtil.isNotEmpty(list)?list:List.of();
    }
    @Override
    public List<UserWithRoleVo> queryByNumberAndRoleCode(String phone,Integer distributeType, String roleCode) {
        List<UserWithRoleVo> list= userMapper.selectUserByPhoneAndRole(phone, distributeType, roleCode);
        if (CollUtil.isNotEmpty(list)){
            list.forEach(a->a.setPhone(DesensitizationUtil.mobileNo(a.getPhone())));
        }
        return CollUtil.isNotEmpty(list)?list:List.of();
    }

    @Override
    public UserWithRoleVo queryUserWithDistributeRole(Long userId) {
        return userMapper.selectUserWithDistributeRole(userId);
    }

    @Override
    public List<UserInfoVo> queryByNumberAndUserName(String phone, String searchValue) {
        return userMapper.selectUserByPhoneAndName(phone,searchValue);
    }

    @Override
    public boolean checkUserIdExist(Long userId) {
        return userMapper.exists(Wrappers.query(UserDO.class).eq("id",userId));
    }
}